package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.order.dao.OrderDeliverySignDao;
import com.wing.order.model.entity.OrderDeliverySign;
import com.wing.order.model.request.OrderDeliverySignForm;
import com.wing.order.model.response.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 签收记录-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderDeliverySignServiceImpl extends ServiceImpl<OrderDeliverySignDao, OrderDeliverySign> implements OrderDeliverySignService {

    @Override
    public int add(OrderDeliverySignForm orderDeliverySignForm) {

        //校验

        OrderDeliverySign orderDeliverySign =new OrderDeliverySign();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderDeliverySign.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderDeliverySignForm, orderDeliverySign, copyOptions);

        return baseMapper.insert(orderDeliverySign);
    }

    @Override
    public List<OrderDeliverySignVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderDeliverySignVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderDeliverySignVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderDeliverySignVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public Integer countNotFinish(Long signId) {
        return baseMapper.countNotFinish(signId);
    }

    @Override
    public IPage<OrderDeliverySignVO> getOrderDeliverySignVOPageList(Integer page, Integer limit,OrderDeliverySignVO orderDeliverySignVO) {
        Page<OrderDeliverySignVO> iPage = new Page<>(page, limit);
        QueryWrapper<OrderDeliverySignVO> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(orderDeliverySignVO.getCode())){
            wrapper.like("si.code",orderDeliverySignVO.getCode());
        }
        if(StringUtils.isNotBlank(orderDeliverySignVO.getDeliveryOrderCode())){
            wrapper.like("delivery_order_code",orderDeliverySignVO.getDeliveryOrderCode());
        }
        if(StringUtils.isNotBlank(orderDeliverySignVO.getStartCreateTime())){
            wrapper.gt("si.create_time", orderDeliverySignVO.getStartCreateTime());
        }
        if(StringUtils.isNotBlank(orderDeliverySignVO.getEndCreateTime())){
            wrapper.le("si.create_time", orderDeliverySignVO.getEndCreateTime());
        }
        wrapper.orderByDesc("si.create_time");
        baseMapper.getOrderDeliverySignVOPageList(iPage,wrapper);
        return iPage;
    }

    @Override
    public IPage<OrderDeliverySignDetailVO> getSignDetailList(Integer page, Integer limit, Long signId) {
        Page<OrderDeliverySignDetailVO> iPage = new Page<>(page, limit);
        QueryWrapper<OrderDeliverySignDetailVO> wrapper = new QueryWrapper<>();
        wrapper.eq("sigd.sign_id",signId);
        wrapper.orderByDesc("sigd.create_time");
        baseMapper.getSignDetailList(iPage,wrapper);
        return iPage;
    }

    @Override
    public IPage<OrderDeliverySignDetailVO> getSignStorageDetailList(Integer page, Integer limit, Long signId) {
        Page<OrderDeliverySignDetailVO> iPage = new Page<>(page, limit);
        QueryWrapper<OrderDeliverySignDetailVO> wrapper = new QueryWrapper<>();
        wrapper.eq("sigd.sign_id",signId);
        wrapper.orderByDesc("sigd.create_time");
        baseMapper.getSignStorageDetailList(iPage,wrapper);
        return iPage;
    }

    @Override
    public WorkbinVO getWorkbinByCode(String code) {
        return baseMapper.getWorkbinByCode(code);
    }

    @Override
    public List<MaterialVO> getMaterialVOListById(Long boxId) {
        return baseMapper.getMaterialVOListById(boxId);
    }

    @Override
    public OrderDeliveryLabelVO getDeliveryLabelVOByCode(String packLabelCode, Long signId) {
        return baseMapper.getDeliveryLabelVOByCode(packLabelCode,signId);
    }

    @Override
    public List<WorkbinVO> getProductBoxByDetailId(Long deliveryDetailId) {
        return baseMapper.getProductBoxByDetailId(deliveryDetailId);
    }

    @Override
    public List<OrderDeliveryLabelVO> getDeliveryLabelVOByBoxId(Long boxId) {
        return baseMapper.getDeliveryLabelVOByBoxId(boxId);
    }

    @Override
    public IPage<OrderDeliverySignVO> getInventorySignVOPageList(Integer page, Integer limit, OrderDeliverySignVO orderDeliverySignVO) {
        Page<OrderDeliverySignVO> iPage = new Page<>(page, limit);
        QueryWrapper<OrderDeliverySignVO> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(orderDeliverySignVO.getCode()),"si.code",orderDeliverySignVO.getCode());
        if(orderDeliverySignVO.getWarehouseId() != null){
            wrapper.eq("i.warehouse_id",orderDeliverySignVO.getWarehouseId());
        }
        wrapper.gt(StringUtils.isNotBlank(orderDeliverySignVO.getStartCreateTime()),"si.create_time", orderDeliverySignVO.getStartCreateTime());
        wrapper.le(StringUtils.isNotBlank(orderDeliverySignVO.getEndCreateTime()),"si.create_time", orderDeliverySignVO.getEndCreateTime());
        wrapper.eq("si.sign_type",1);
        wrapper.orderByDesc("si.create_time");
        baseMapper.getInventorySignVOPageList(iPage,wrapper);
        return iPage;
    }

    @Override
    public List<OrderDeliverySignVO> abnormalList(Long id) {
        List<OrderDeliverySignVO> abnormalList = baseMapper.abnormalList(id);
        for (int i = 0; i <abnormalList.get(0).getDetailList().size() ; i++) {
            if (abnormalList.get(0).getDetailList().get(i).getQuantityAbnormal()<=0){
                abnormalList.get(0).getDetailList().remove(i);
            }
        }
        return abnormalList;
    }

    @Override
    public Page<OrderDeliverySignVO> getPurchaseSignVOPageList(Page page, Map<String, Object> params) {

        List<OrderDeliverySignVO> purchaseSignVOPageList = baseMapper.getPurchaseSignVOPageList(page,params);
        page.setRecords(purchaseSignVOPageList);
        return page;
    }

    @Override
    public OrderDeliverySignVO getDeliverySignInfo(String code, Integer signType) {
        OrderDeliverySignVO signInfo = baseMapper.getDeliverySignInfo(code, signType);
        return signInfo;
    }


    @Override
    public OrderDeliverySignVO getPurchaseSignVOById(Long id) {
        //采购数据
        OrderDeliverySignVO signVO = baseMapper.getPurchaseSignVOById(id);
        //产品数据
        List<OrderDeliverySignDetailVO> detailVOList = baseMapper.getPurchaseSignDetailVO(signVO.getId());
        for (int i = 0; i <detailVOList.size() ; i++) {
            //向上取整 设置包装条码数量
            double packNum = Math.ceil((double)detailVOList.get(i).getNum() / detailVOList.get(i).getBaseRate());
            detailVOList.get(i).setPackNum((int)packNum);
            signVO.setDetailList(detailVOList);
        }
        return signVO;
    }


/*    @Override
    public List<OrderDeliverySignDetailVO> getPurchaseSignDetailVO(Long id) {
        List<OrderDeliverySignDetailVO> detailVOS = baseMapper.getPurchaseSignDetailVO(id);
        for (int i = 0; i <detailVOS.size() ; i++) {
            //向上取整 设置包装条码数量
            double packNum = Math.ceil(detailVOS.get(i).getNum() / detailVOS.get(i).getBaseRate());
            detailVOS.get(i).setPackNum((int)packNum);
        }
        return detailVOS;
    }*/
}
